import {formatDate, formatScheduleDate} from "../utils/dateService";
import {formatAsiaOdd} from "../containers/game-room/components/Main/index/AsiaOddsTable/AsiaOddsTable";

class GameAIModel {
    constructor(game) {
        this.game = game
    }

    checkRateArr(arr) {
        if (arr === null || arr === 0) return [0, 0, 0,]
        return arr
    }

    parseFloatToPercent = float => {
        // console.log(float)
        const to2 = float.toFixed(4)
        if (Number.isNaN(to2)) return ''
        const arr = to2.split('.')
        const floatString = arr[1]
        if (floatString === undefined) return ''
        let result = Number(floatString.slice(0, 2))
        const last = Number(floatString.slice(2, 4))
        if (last > 50) {
            result = result + 1
        } else {
            // result = result - 1
        }
        // const result = Number(to2.split('.')[1])
        return result
    }

    formatPan = pan => {
        return  formatAsiaOdd(pan)
        let result
        let rate = pan / 0.25
        if (rate % 2 === 0) {
            result = pan.toString()
        } else {
            let down = pan - 0.25
            let up = pan + 0.25
            result = `${down}/${up}`
        }
        return result
    }

    detail() {
        let game = this.game
        let {
            // state,
            // namiid,
            // id,
            // startballt,
            // kelogo,
            // zhulogo,
            // qingbao,
            // kefen,
            // zhufen,
            // Result,
            Id: id,
            Result,
            Matchname: name,
            Startt: startt,
            Zhuname: zhuname,
            Kename: kename,
            State: state,
            Namiid: namiid,
            Startballt: startballt,
            Kelogo: kelogo,
            Zhulogo: zhulogo,
            Qingbao: qingbao,
            Kefen: kefen,
            Zhufen: zhufen,
        } = game

        const cName = name[0]
        const date = new Date(startt * 1000)
        const todayDate = new Date()
        let MonthDay = formatDate(date).slice(4, 6) + '月' + formatDate(date).slice(6) + '日'
        let todayMonthDay = formatDate(todayDate).slice(4, 6) + '月' + formatDate(todayDate).slice(6) + '日'
        let time = formatScheduleDate(date)

        const hostName = zhuname[0]
        const awayName = kename[0]
        let showStyleType
        if (Result === 1 || Result === 11 || Result === 21) {
            showStyleType = 1
        } else if (Result === 2 || Result === 22) {
            showStyleType = 2
        } else if (Result === 3 || Result === 23) {
            showStyleType = 3
        }
        return {
            info: qingbao,
            c: cName,
            status: state,
            host: {
                name: hostName,
                logo: zhulogo,
                score: zhufen,
            },
            away: {
                name: awayName,
                logo: kelogo,
                score: kefen,
            },
            time,
            date: MonthDay,
            matchId: namiid,
            eventId: id,
            schedule: startt,
            startAt: startballt,
            isToday: MonthDay === todayMonthDay,
            showStyleType: showStyleType,
        }
    }

    RQ() {
        let game = this.game
        const {
            Yuce: rangqiu,
            Result: rqresult,
        } = game
        let RQTitle = '让球'
        let RQSubTitle
        let resultRate
        let RQ = this.checkRateArr(rangqiu)
        let RQResult = rqresult
        const RQHomeRate = RQ[0]
        const RQDate = RQ[1]
        const RQAwayRate = RQ[2]
        const RQTotal = RQHomeRate + RQAwayRate
        let RQHome = this.parseFloatToPercent(RQAwayRate / RQTotal)
        // let RQHome = Number((RQAwayRate / RQTotal).toFixed(2)) * 100
        // console.log(RQHome)
        let RQAway = this.parseFloatToPercent(RQHomeRate / RQTotal)
        if (RQHome === RQAway) {
            if (RQHomeRate > RQAwayRate) {
                RQHome--
                RQAway++
            } else {
                RQHome++
                RQAway--
            }
        }
        if (RQHome > RQAway) {
            RQSubTitle = '主胜'
            resultRate = RQHome
        } else {
            RQSubTitle = '客胜'
            resultRate = RQAway
        }
        // console.log('让球', resultRate)
        return {
            title: RQTitle,
            oddsArr: RQ,
            subTitle: RQSubTitle,
            result: RQResult,
            resultRate,
            pan: this.formatPan(RQDate),
            home: RQHome,
            away: RQAway,
            tied: 0,
        }
    }

    SFP() {
        let SFPTitle = '胜负平'
        let game = this.game
        let {
            Yuce: shengps,
            Result: spsresult,
        } = game
        let SFP = this.checkRateArr(shengps)
        // let [SFPHomeRate, SFPTiedRate, SFPAwayRate,] = SFP
        let SFPResult = spsresult
        let temp = SFP.sort((a, b) => b - a)
        let first = temp[0]
        let second = temp[1]
        let third = temp[2]
        let total = first + second + third

        let firstPer = this.parseFloatToPercent(first / total)
        let secondPer = this.parseFloatToPercent(second / total)
        let thirdPer = this.parseFloatToPercent(third / total)

        let firstIndex = SFP.findIndex(item => item === first)
        let secondIndex = SFP.findIndex(item => item === second)
        let thirdIndex = SFP.findIndex(item => item === third)

        let perArr = []
        if (firstPer === secondPer) {
            firstPer++
            secondPer--
        } else if (secondPer === thirdPer) {
            secondPer++
            thirdPer--
        }
        perArr[firstIndex] = thirdPer
        perArr[secondIndex] = secondPer
        perArr[thirdIndex] = firstPer
        let [SFPHome, SFPTied, SFPAway,] = perArr

        // let SFPTotal = SFPHomeRate + SFPTiedRate + SFPAwayRate
        // let SFP1 = this.parseFloatToPercent(SFPHomeRate / SFPTotal)
        // let SFP2 = this.parseFloatToPercent(SFPTiedRate / SFPTotal)
        // let SFP3 = this.parseFloatToPercent(SFPAwayRate / SFPTotal)
        // let SArray = [SFP1, SFP2, SFP3]
        // let sortedS = [...SArray].sort((a, b) => a - b)
        // const smallest = sortedS[0]
        // const middle = sortedS[1]
        // const largest = sortedS[2]
        //
        // const smallestIndex = SArray.findIndex(e => e === smallest)
        // const largestIndex = SArray.findIndex(e => e === largest)
        // SArray[smallestIndex] = largest
        // SArray[largestIndex] = smallest
        // let [SFPHome, SFPTied, SFPAway] = SArray

        return {
            title: SFPTitle,
            oddsArr: SFP,
            // subTitle: RQSubTitle,
            result: SFPResult,
            first: firstPer,
            second: secondPer,
            third: thirdPer,
            home: SFPHome,
            away: SFPAway,
            tied: SFPTied,
        }
    }

    DXQ() {
        let game = this.game
        let {
            Result: dxresult,
            Yuce: daxiao,
        } = game
        let DXQ = this.checkRateArr(daxiao)
        let DXQTitle = '大小球'
        let DXQSubTitle
        let resultRateDXQ
        const DXQHomeRate = DXQ[0]
        const DXQAwayRate = DXQ[2]
        const DXQResult = dxresult
        const DXQTotal = DXQHomeRate + DXQAwayRate
        let DXQHome = this.parseFloatToPercent(DXQAwayRate / DXQTotal)
        // cons
        // console.log(RQHome)
        let DXQAway = this.parseFloatToPercent(DXQHomeRate / DXQTotal)
        if (DXQHome > DXQAway) {
            DXQSubTitle = '大球'
            resultRateDXQ = DXQHome
        } else {
            DXQSubTitle = '小球'
            resultRateDXQ = DXQAway
        }
        return {
            title: DXQTitle,
            oddsArr: DXQ,
            subTitle: DXQSubTitle,
            result: DXQResult,
            resultRate: resultRateDXQ,
            home: DXQHome,
            away: DXQAway,
            tied: 0,
        }
    }
}

export default GameAIModel
